package com.sunny.shop.api.v1;

import com.sunny.shop.core.interceptors.ScopeLevel;
import com.sunny.shop.exception.httpe.NotFoundException;
import com.sunny.shop.model.GoodsOrder;
import com.sunny.shop.service.GoodsOrderServiceImpl;
import com.sunny.shop.util.CommonUtil;
import com.sunny.shop.vo.GoodsOrderDetailVO;
import com.sunny.shop.vo.GoodsOrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private GoodsOrderServiceImpl goodsOrderServiceImpl;

    @ScopeLevel()
    @GetMapping("/list")
    public List<GoodsOrderVO> getOrders(){
        List<GoodsOrderVO> orderList = goodsOrderServiceImpl.getOrderList();
        return orderList;
    }

    @ScopeLevel
    @GetMapping("/{order_sn}")
    public GoodsOrderDetailVO getOrderDetail(@PathVariable String order_sn){
        GoodsOrder orderDetail = goodsOrderServiceImpl.getOrderDetail(order_sn);
        if(orderDetail == null){
            throw new NotFoundException(50000);
        }
        GoodsOrderDetailVO goodsOrderDetailVO = new GoodsOrderDetailVO();
        BeanUtils.copyProperties(orderDetail, goodsOrderDetailVO);
        goodsOrderDetailVO.setCreateTime(CommonUtil.getSimpleData(orderDetail.getCreateTime()));
        return goodsOrderDetailVO;
    }
}
